#include <sys/time.h>
#include <string.h>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#define DEBUG 1
#define REFRESH_INTVL 1000 //in tens of millis

//remove if you please
extern int errno;

extern struct timeval begining_of_time;
extern unsigned long myjiffies;
extern struct timeval myxtime;

void handleAlarm(int t){
  myjiffies++;
  if(myjiffies % REFRESH_INTVL == 0){
    struct timeval act;
    int tJIFFIES = myjiffies;
    gettimeofday(&act, NULL);
    if(act.tv_usec < begining_of_time.tv_usec) {
      myjiffies = (act.tv_sec - begining_of_time.tv_sec - 1) * 100 
	+ (1000000 + act.tv_usec - begining_of_time.tv_usec)/10000;
      if (DEBUG)
	printf("jiffies was: %d, is %d\n", tJIFFIES, myjiffies);

    }else {
      myjiffies = (act.tv_sec - begining_of_time.tv_sec) * 100
	+ (act.tv_usec - begining_of_time.tv_usec)/10000;
      if (DEBUG)
	printf("jiffies was: %d, is %d\n", tJIFFIES, myjiffies);
    }
  }
  myxtime.tv_sec = myjiffies/100;
  myxtime.tv_usec = (myjiffies*10000)%100000;
}
